<template>
  <!-- 空间使用排行分析 -->
  <div id="space-rank-analyze">
    <a-card title="空间使用排行">
      <v-chart :option="option" style="height: 320px" :loading="loading"></v-chart>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref, withDefaults, defineProps, onMounted, computed } from 'vue'
import {
  getSpaceRankAnalyzeUsingPost,
  getSpaceTagAnalyzeUsingPost,
} from '@/request/api/spaceAnalyzeController.ts'
import { message } from 'ant-design-vue'
import 'echarts-wordcloud'

interface Props {
  queryAll?: boolean
  queryPublic?: boolean
  spaceId?: number
}

const props = withDefaults(defineProps<Props>(), {
  queryAll: false,
  queryPublic: false,
})

const dataList = ref<API.Space[]>([])
const loading = ref(false)

// 获取空间使用排行,默认前十
const fetchData = async () => {
  loading.value = true
  const { data: res } = await getSpaceRankAnalyzeUsingPost({
    queryAll: props.queryAll,
    queryPublic: props.queryPublic,
    spaceId: props.spaceId,
  })
  if (res.code === 0 && res.data) {
    dataList.value = res.data
  } else {
    message.error('获取数据失败,' + res.message)
  }
  loading.value = false
}
const option = computed(() => {
  const spaceNames = dataList.value.map((item) => item.spaceName)
  const usageData = dataList.value.map((item) => ((item.totalSize ?? 0) / (1024 * 1024)).toFixed(2)) // 转为 MB

  return {
    tooltip: { trigger: 'axis' },
    xAxis: {
      type: 'category',
      data: spaceNames,
    },
    yAxis: {
      type: 'value',
      name: '空间使用量 (MB)',
    },
    series: [
      {
        name: '空间使用量 (MB)',
        type: 'bar',
        data: usageData,
        itemStyle: {
          color: '#5470C6', // 自定义柱状图颜色
        },
      },
    ],
  }
})

onMounted(() => {
  fetchData()
})
</script>

<style lang="less" scoped></style>
